/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.facilscrum.form.sprint;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.facilscrum.beans.Proyectos;
import org.facilscrum.beans.Sprint;
import org.facilscrum.beans.Sprintmeeting;
import org.facilscrum.beans.Sprintretrospective;
import org.facilscrum.controller.SprintController;
import org.facilscrum.controller.SprintretrospectiveController;
import org.facilscrum.generales.Mensajeria;
import org.facilscrum.sesion.MySesion;
import org.javscaz.javscaztools.utilidades.Utilidades;

/**
 *
 * @author avbravo
 */
public class SprintRetrospectiveForm extends javax.swing.JInternalFrame {
    
    Proyectos proyectos = new Proyectos();
    Sprint sprint = new Sprint();
    SprintController sprintController = new SprintController();
    Sprintretrospective sprintretrospective = new Sprintretrospective();
    SprintretrospectiveController sprintretrospectiveController = new SprintretrospectiveController();
    List<Sprint> listSprint = new ArrayList<>();

    /**
     * Creates new form DailyScrumForm
     */
    public SprintRetrospectiveForm() {
        initComponents();
        if (!MySesion.getSeleccionoProyectoTrabajo()) {
            Mensajeria.MensajeWarning("Debe indicar el proyecto de trabajo");
        } else {
            proyectos = MySesion.getProyectos();
            jTextFieldProyecto.setText(proyectos.getProyecto());
            cargarComboSprint();
            java.util.Calendar ca = java.util.Calendar.getInstance();
            java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());
            jXDatePickerFecha.setDate(mydate);
            jXDatePickerFecha.setFormats(new SimpleDateFormat("dd/MM/yyyy"));
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaBueno = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jComboBoxSprint = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaMalo = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextAreaMejorar = new javax.swing.JTextArea();
        jTextFieldProyecto = new javax.swing.JTextField();
        jButtonGuardar = new javax.swing.JButton();
        jButtonCerrar = new javax.swing.JButton();
        jXDatePickerFecha = new org.jdesktop.swingx.JXDatePicker();

        setClosable(true);
        setTitle("Sprint Retrospective");

        jLabel1.setText("Proyecto");

        jLabel2.setText("Fecha");

        jLabel3.setText("Bueno");

        jTextAreaBueno.setColumns(20);
        jTextAreaBueno.setRows(5);
        jScrollPane1.setViewportView(jTextAreaBueno);

        jLabel4.setText("Sprint");

        jComboBoxSprint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxSprintActionPerformed(evt);
            }
        });

        jLabel5.setText("Malo");

        jTextAreaMalo.setColumns(20);
        jTextAreaMalo.setRows(5);
        jScrollPane2.setViewportView(jTextAreaMalo);

        jLabel6.setText("Mejorar");

        jTextAreaMejorar.setColumns(20);
        jTextAreaMejorar.setRows(5);
        jScrollPane3.setViewportView(jTextAreaMejorar);

        jTextFieldProyecto.setEnabled(false);

        jButtonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/facilscrum/gif/Save.png"))); // NOI18N
        jButtonGuardar.setToolTipText("Guardar");
        jButtonGuardar.setEnabled(false);
        jButtonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarActionPerformed(evt);
            }
        });

        jButtonCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/facilscrum/gif/return.png"))); // NOI18N
        jButtonCerrar.setToolTipText("Regresar");
        jButtonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCerrarActionPerformed(evt);
            }
        });

        jXDatePickerFecha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXDatePickerFechaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(23, 23, 23)
                                .addComponent(jLabel1))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel4))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel2)))
                        .addGap(28, 28, 28)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextFieldProyecto)
                            .addComponent(jXDatePickerFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBoxSprint, 0, 283, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(30, 30, 30)
                                .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(30, 30, 30)
                                .addComponent(jButtonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel3)
                            .addComponent(jLabel5))))
                .addContainerGap(92, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldProyecto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBoxSprint, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jXDatePickerFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addGap(98, 98, 98)
                        .addComponent(jLabel6))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonGuardar)
                    .addComponent(jButtonCerrar)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jXDatePickerFechaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXDatePickerFechaActionPerformed
        // jButtonGuardar.setEnabled(noExiste());
    }//GEN-LAST:event_jXDatePickerFechaActionPerformed
    
    private void jButtonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCerrarActionPerformed
        dispose();
    }//GEN-LAST:event_jButtonCerrarActionPerformed
    
    private void jComboBoxSprintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxSprintActionPerformed
        search();
    }//GEN-LAST:event_jComboBoxSprintActionPerformed
    
    private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGuardarActionPerformed
        guardar();
    }//GEN-LAST:event_jButtonGuardarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCerrar;
    private javax.swing.JButton jButtonGuardar;
    private javax.swing.JComboBox jComboBoxSprint;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextAreaBueno;
    private javax.swing.JTextArea jTextAreaMalo;
    private javax.swing.JTextArea jTextAreaMejorar;
    private javax.swing.JTextField jTextFieldProyecto;
    private org.jdesktop.swingx.JXDatePicker jXDatePickerFecha;
    // End of variables declaration//GEN-END:variables

    private Boolean cargarComboSprint() {
        try {
            listSprint.removeAll(listSprint);
            jComboBoxSprint.removeAllItems();
            String where = "idproyecto='" + proyectos.getIdproyecto() + "' and abierto='si' ";
            if (sprintController.query(where)) {
                for (Sprint s : sprintController.getSprintlist()) {                    
                    if (noExiste(s)) {
                        jComboBoxSprint.addItem(s.getIdsprint() + " - " + s.getNombre());
                        Sprint s1 = new Sprint();
                        s1 = s;
                        listSprint.add(s1);
                    }
                }
                
            }
            if (jComboBoxSprint.getItemCount() == 0) {
                habilitar(false);
                
                return false;
            }
            return true;
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "cargarComboSprint() ");
        }
        return false;
    }
    
    private void guardar() {
        try {
            
            java.util.Date Fecha = jXDatePickerFecha.getDate();
            java.sql.Date fecha = new java.sql.Date(Fecha.getTime());
            if (fecha == null) {
                Mensajeria.MensajeWarning("Seleccione la fecha");
                jXDatePickerFecha.requestFocus();
                return;
            }
            if (jTextAreaBueno.getText() == null || jTextAreaBueno.getText().equals("")) {
                Mensajeria.MensajeWarning("Ingrese el comentario acerca de las situaciones buenas...");
                jTextAreaBueno.requestFocus();
                return;
            }
            if (jTextAreaMalo.getText() == null || jTextAreaMalo.getText().equals("")) {
                Mensajeria.MensajeWarning("Ingrese el comentario acerca de las situaciones malas..");
                jTextAreaMalo.requestFocus();
                return;
            }
            
            if (jTextAreaMejorar.getText() == null || jTextAreaMejorar.getText().equals("")) {
                Mensajeria.MensajeWarning("Ingrese el comentario acerca de las situaciones a  mejorar..");
                jTextAreaMejorar.requestFocus();
                return;
            }
            
            sprintretrospective.setBueno(jTextAreaBueno.getText());
            sprintretrospective.setMalo(jTextAreaMalo.getText());
            sprintretrospective.setMejorar(jTextAreaMejorar.getText());
            sprintretrospective.setFecha(fecha);
            sprintretrospective.setIdsprint(sprint.getIdsprint());
            if (!sprintretrospectiveController.insert(sprintretrospective)) {
                Mensajeria.MensajeWarning("No se puede insertar el registro " + sprintretrospectiveController.getMensaje());
                return;
            }
            Mensajeria.Mensaje("Proceso terminado");
            cargarComboSprint();
            limpiar();
            
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "guardar");
        }
    }
    
    private void search() {
        try {
           
            if(listSprint.size()== 0){
                return;
            }
            int j = jComboBoxSprint.getSelectedIndex();
            if (j != -1) {
                sprint = listSprint.get(j);                
                habilitar(true);
            }
            
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "search()");
        }
    }
    
    private Boolean noExiste(Sprint s) {
        try {
            String sql = "idsprint='" + s.getIdsprint() + "'";
            if (!sprintretrospectiveController.query(sql)) {
                return true;
            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "noExiste()");
        }
        return false;
    }
    
    private void habilitar(Boolean valor) {
        try {
            jTextAreaBueno.setEnabled(valor);
            jTextAreaMalo.setEnabled(valor);
            jTextAreaMejorar.setEnabled(valor);
            jTextAreaBueno.setEditable(valor);
            jTextAreaMalo.setEditable(valor);
            jTextAreaMejorar.setEditable(valor);
            jXDatePickerFecha.setEnabled(valor);
            jComboBoxSprint.setEnabled(valor);
            jButtonGuardar.setEnabled(valor);
            
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "deshabilitar()");
        }
    }
    
    private void limpiar() {
        jTextAreaBueno.setText("");
        jTextAreaMalo.setText("");
        jTextAreaMejorar.setText("");
    }
}
